/*
 * Copyright (C) 2004, Velocityme Partnership
 * TaskStateMachineValueActionForm.java
 *
 * Created on 09 October 2004, 02:34
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.Role;
import com.velocityme.entity.TaskStateMachine;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.RoleLocal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class TaskStateMachineActionForm extends NodeActionForm {
    
    private Long[] m_unassignedRoleIds;
    private Long[] m_interestedRoleIds;

    public void extractTaskStateMachine(KeyLocal key, TaskStateMachine taskStateMachine) {
        extractNode(taskStateMachine);
        taskStateMachine.clearInterestedRoles();
        RoleLocal roleLocal = lookupRoleBean();
        if (m_interestedRoleIds != null) {
            for (Long id: m_interestedRoleIds) {
                Role role = roleLocal.findRole(key, id);
                taskStateMachine.addInterestedRole(role);
            }
        }
    }

    public Long[] getUnassignedRoleIds() {
        return m_unassignedRoleIds;
    }
    
    public void setUnassignedRoleIds(Long[] unassignedRoleIds) {
        m_unassignedRoleIds = unassignedRoleIds;
    }
    
    public Long[] getInterestedRoleIds() {
        return m_interestedRoleIds;
    }
    
    public void setInterestedRoleIds(Long[] interestedRoleIds) {
        m_interestedRoleIds = interestedRoleIds;
    }
    
    private RoleLocal lookupRoleBean() {
        try {
            Context c = new InitialContext();
            return (RoleLocal) c.lookup("java:comp/env/RoleBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
